package cn.wolfcode.crud.web.controller;


import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.domain.Notice;
import cn.wolfcode.crud.service.INoticeService;
import cn.wolfcode.crud.qo.QuerObjcet;
import cn.wolfcode.crud.qo.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiresPermissions(value = {"notice:list","公告通知管理明细页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QuerObjcet qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("PageResult", pageInfo);
        return "notice/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"notice:delete","公告通知管理明细删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"notice:saveOrUpdate","公告通知管理明细新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
        }else {
            noticeService.save(notice);
        }
        return new JsonResult();
    }
    @RequestMapping("/skip")
    public String skip(Model model,Notice notice){
        //修改状态
        noticeService.update(notice);
        //还有添加中间表单


        //拿到登陆用户名字
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("employee",employee);
        Notice notice1 = noticeService.get(notice.getId());
        model.addAttribute("notice1",notice1);
        return "notice/show";
    }
}
